<template>
	<view>
		<view style="background-color: #FFFFFF;">
			<cu-custom :isBack="true">
				<block slot="backText"></block>
				<block slot="content">{{corpCode}}-产成品上架</block>
			</cu-custom>
		</view>
		<view class="cu-bar bg-white search fixed indexes" :style="[{top:CustomBar + 'px'}]">
			<view class="search-form round">
				<text class="cuIcon-search"></text>
				<input @confirm="sub" v-model="moId" type="text" placeholder="输入搜索的关键词" focus></input>
			</view>
			<view class="action">
				<button class="cu-btn bg-gradual-green shadow-blur round" @click="clerInv">清空</button>
			</view>
			<view class="action" @click="toScan">
				<text class="cuIcon-scan" style="font-size: 56rpx;"></text>
			</view>
		</view>
		<view style="font-size: 16px; background-color: #fff;height: 100%;margin-top: 10%;">
			<view class="solids-bottom" style="height: 50%;">
				<view class="flex-sub text-center solid-bottom text-xl padding">
					<text class="text-black text-bold">货位信息</text>
				</view>
				<view class="padding">
					<text>货位名称：{{hwname}}</text>
				</view>
			</view>
			<view style="height: 50%;">
				<view class="flex-sub text-center solid-bottom text-xl padding">
					<text class="text-black text-bold">生产订单</text>
				</view>
					<view class="content padding" v-for="(item, index) in inv" :key="item.index">
						<view class="action" style="float: right;">
							<button class="cu-btn bg-gradual-green shadow-blur round"  @tap="showModal" data-target="DrawerModalR">推荐货位</button>
						</view>
						<view style="margin-bottom: 10px;">
							<text class="text-blue margin-right-xs"></text> 订单单号：{{item.mocode}}
						</view>
						<view style="margin-bottom: 10px;">
							<text class="text-blue margin-right-xs"></text> 订单行号：{{item.sortseq}}
						</view >
						<view style="margin-bottom: 10px;">
							<text class="text-blue margin-right-xs"></text> 存货编码：{{item.cinvcode}}
						</view>
						<view style="margin-bottom: 10px;">
							<text class="text-blue margin-right-xs"></text> 存货名称：{{item.cinvname}}
						</view>
						<!-- <view>
							<text class="text-blue margin-right-xs"></text> 生产数量：{{item.qty}}
						</view> -->
						<view class="text-gray cu-form-group" style="padding: 10px 0px 0px 0px;">
							<text class=" margin-right-xs" style="color: red;font-weight: 1000;">上架数量：</text>
							<input placeholder="0" name="input" @input="onInput" type="number" v-model="item.prtqty"></input>
						</view>
					</view>
			</view>
		</view>
		<view class="action-section">
			<button type="primary" class="no-border confirm-btn" :disabled="isReC" @click="createOrder">上架</button>
		</view>
		<view class="cu-modal drawer-modal justify-end" :class="modalName=='DrawerModalR'?'show':''" @tap="hideModal">
			<view class="cu-dialog basis-lg" @tap.stop="" :style="[{top:CustomBar+'px',height:'calc(100vh - ' + CustomBar + 'px)',width:'80%'}]">
				<view>
					<view v-if="cHList.length<1" class="nodata">暂无数据</view>
					<view class="cu-list menu"  v-for="(item,index) in cHList" :key="index">
						<view class="cu-item" >
							<view class="content padding-tb-sm">
								
								<view class="text-black text-sm view-s"  style="text-align: left;">仓库：<text  class="t-text"  style="float: right;"> {{item.cwhname}}</text></view>
								<view class="text-black text-sm view-s"  style="text-align: left;">货位：<text  class="t-text" > {{item.cposname}}</text></view>
								<view class="text-black text-sm view-s"  style="text-align: left;">数量：<text  class="t-text" > {{item.iquanttity}}</text></view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	export default {
		data() {
			return {
				options1: [{
					text: '删除'
				}],
				CustomBar: this.CustomBar, //滚动定位用
				modalName:null,
				cHList:[],
				hwname:"",
				moId:"",
				hwcode:'',
				inv:[{
					mocode:'',
					sortseq:'',
					cinvcode:'',
					cinvname:'',
					qty:'',
					prtqty:'',
					maySjNum:'',//可上架数量
					sjNum:1,//本次上架数量
				}],
				moData:[],
				isReC:false,
			}
		},
		computed: {
			...mapState(['hasLogin', 'userInfo', 'auditlist', 'corpCode']),
		},
		onLoad() {
			// this.moId = 'hw_112_16001';
			// this.sub();
			//this.moId = 'sc_102_1000039003_2';
			//this.sub();
		},
		methods: {
			/** 扫发出声音*/
			makeSound(name){
				console.log("=====testClick=====");
				let src = '../../static/'+name+'.mp3';
				//实例化声音  
				const Audio = uni.createInnerAudioContext();
				Audio.autoplay = true;
				Audio.src = src; //音频地址  
				Audio.play(); //执行播放  
				Audio.onError((res) => {
				});
				Audio.onPause(function() {
					console.log('end');
					Audio.destroy();
				});
			},
			
			showModal(e) {
				this.modalName = e.currentTarget.dataset.target
				this.findHWData();
			},
			hideModal(e) {
				this.modalName = null
			},
			findHWData(){
				let cinvcode=this.inv[0].cinvcode;
				if(cinvcode == undefined || cinvcode == ''){
					return;
				}
				uni.request({
					url:this.serverURL_a+'/wh/u8/u8InvpositionSum/sumListData?__ajax=json',
					data:{
						cinvcode : cinvcode,
						"orderBy":"cbatch desc",
						__sid:this.userInfo.sessionid
					},
					header: {
						'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
					},
					method: 'POST',
					success:function(data){
						let ret = data.data;
						if(data.statusCode!="200"){
							uni.showModal({
								content: ret.message,
								showCancel:false,
							});
							return;
						}
						this.cHList=data.data;
					}.bind(this),
					fail:function(){
						this.$api.msg('服务已停止,请稍后再试');
						return;
					}.bind(this),
				});
			},
			hasLogins(){
				if(!this.hasLogin){
					uni.navigateTo({
						url: '/pages/public/login'
					})
					return;
				} 
			},
			onInput(e){
				this.sjNum = e.target.value
			},
			//加载数据
			onloadData(){
				this.hasLogins();
				uni.request({
					// url:this.serverURL_a+'/mo/AppApi/getMoOrderData',
					url:this.serverURL_a+'/barcode/decode/getMoOrderInfo',
					data:{
						// id:this.moId,
						__sid:this.userInfo.sessionid,
						barCode: this.moId,
						bneedStore: '0',
						bmustAcct:'1',
						barType:'1'
					},
					header: {
						'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
					},
					method: 'POST',
					success:function(data){
						this.moId = '';
						var orders = data.data;
						
						if(orders.result=="false"){
							uni.showModal({
							  content: orders.message,
							  showCancel:false,
							})
							return false;
						}
						
						var orderList = orders.data.moOrder;
						if(this.moData.length < 1){
							this.moData.push(orderList);
							orderList.prtqty = 1;
						}else{
							var i=0;
							for(;i<this.moData.length;i++){ 
								if(orderList.modid == this.moData[i].modid ){
									this.moData[i].prtqty += 1;
									break;
								}
							}
							if(i>=this.moData.length){
								this.moData = [];
								this.moData.push(orderList);
								orderList.prtqty = 1;
							}
						}
						for(var j=0;j<this.moData.length;j++){
							this.inv[j].mocode = this.moData[j].mocode;
							this.inv[j].sortseq = this.moData[j].sortseq;
							this.inv[j].cinvcode = this.moData[j].basInventory.cinvcode;
							this.inv[j].cinvname = this.moData[j].basInventory.cinvname;
							this.inv[j].qty = this.moData[j].qty;
							this.inv[j].prtqty = this.moData[j].prtqty;
						}
						
						
						// if(orders.code == '1001'){
						// 	var orderList = orders.resMap;
						// 	if(this.moData.length < 1){
						// 		this.moData.push(orderList);
						// 		orderList.prtqty = 1;
						// 	}else{
						// 		var i=0;
						// 		for(;i<this.moData.length;i++){ 
						// 			if(orderList.modid == this.moData[i].modid && orderList.acctId == this.moData[i].acctId){
						// 				this.moData[i].prtqty += 1;
						// 				break;
						// 			}
						// 		}
						// 		if(i>=this.moData.length){
						// 			this.moData = [];
						// 			this.moData.push(orderList);
						// 			orderList.prtqty = 1;
						// 		}
						// 	}
						// 	for(var j=0;j<this.moData.length;j++){
						// 		this.inv[j].mocode = this.moData[j].momOrderdetail.mocode;
						// 		this.inv[j].sortseq = this.moData[j].momOrderdetail.sortseq;
						// 		this.inv[j].cinvcode = this.moData[j].momOrderdetail.basInventory.cinvcode;
						// 		this.inv[j].cinvname = this.moData[j].momOrderdetail.basInventory.cinvname;
						// 		this.inv[j].qty = this.moData[j].qty;
						// 		this.inv[j].prtqty = this.moData[j].prtqty;
						// 	}
						// }else{
						// 	uni.showModal({
						// 	    content: orders.msg,
						// 		showCancel:false,
						// 	});
						// 	return;
						// }
					}.bind(this),
					fail: function() {
						this.$api.msg('无法连接服务，请联系管理');
						this.isReC = false;
						return;
					}.bind(this)
				})
			},
			//扫码枪扫描条码或按下回车调用
			sub(){
				this.hasLogins();
				if(this.moId.indexOf("hw") > -1){
					uni.request({
						url:this.serverURL_a+'/so/App/getHw',
						data:{
							hw : this.moId,
							__sid:this.userInfo.sessionid
						},
						header: {
							'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
						},
						method: 'POST',
						success:function(data){
							this.moId = '';
							var requrt = data.data;
							if(requrt.code == '1001'){
								var hwdata = requrt.resMap;
								this.hwcode = hwdata.cposcode;
								this.hwname = hwdata.cposname;
							}else{
								this.inv = [{
									mocode:'',
									sortseq:'',
									cinvcode:'',
									cinvname:'',
									qty:'',
									prtqty:'',
								}];
								uni.showModal({
								    content: requrt.msg,
									showCancel:false,
								});
								return;
							}
						}.bind(this),
						fail:function(){
							this.inv = [{
								mocode:'',
								sortseq:'',
								cinvcode:'',
								cinvname:'',
								qty:'',
								prtqty:'',
							}];
							this.$api.msg('服务已停止,请稍后再试');
							return;
						}.bind(this),
					});
					return;
				// }else if(this.moId == '' || this.moId.indexOf("sc") == -1){
				}else if(this.moId == ''){
					this.moId = '';
					this.inv = [{
						mocode:'',
						sortseq:'',
						cinvcode:'',
						cinvname:'',
						qty:'',
						prtqty:'',
					}];
					uni.showModal({
					    content: "请扫描正确的条码!",
						showCancel:false,
					});
					this.moId = '';
					return;
				}
				this.onloadData();
				if(this.hwcode == ''){
					uni.showModal({
					    content: "请扫描货位条码！",
						showCancel:false,
					});
					return;
				}
			},
			//提交数据
			createOrder(){
				this.hasLogins();
				//判断是否将可以提交
				if(this.inv[0].mocode == '' || this.hwcode == ''){
					uni.showModal({
						content: '请扫描标签,货位不能为空!',
						showCancel:false,
					});
					this.makeSound("sb");
					return;
				}
				if(this.isReC){
					this.$api.msg('请不要重复提交！');
					this.makeSound("sb");
					return;
				}
				this.isReC = true;
				let invs = [{
					moid:'',
					modid:'',
					mocode:'',
					prtqty:'',
					sortseq:'',
					partid:'',
					moclass:'',
					hw:'',
				}];
				// for(var i= 0;i<this.moData.length;i++){
				// 	invs[i].moid = this.moData[i].moid;
				// 	invs[i].modid = this.moData[i].modid;
				// 	invs[i].mocode = this.moData[i].momOrderdetail.mocode;
				// 	invs[i].prtqty = this.inv[i].prtqty;
				// 	invs[i].sortseq = this.moData[i].momOrderdetail.sortseq;
				// 	invs[i].partid = this.moData[i].momOrderdetail.basInventory.cinvcode;
				// 	invs[i].moclass = this.moData[i].momOrderdetail.moclass;
				// 	invs[i].hw = this.hwcode;
				// }
				for(var i= 0;i<this.moData.length;i++){
					invs[i].moid = this.moData[i].moid;
					invs[i].modid = this.moData[i].modid;
					invs[i].mocode = this.moData[i].mocode;
					invs[i].prtqty = this.inv[i].prtqty;
					invs[i].sortseq = this.moData[i].sortseq;
					invs[i].partid = this.moData[i].basInventory.cinvcode;
					invs[i].moclass = this.moData[i].moclass;
					invs[i].hw = this.hwcode;
				}
				let inv = JSON.stringify(invs);
				//提交数据
				uni.showModal({
					//content: '是否提交!\r\n当前上架数量：'+this.sjNum+';可上架数量:'+this.maySjNum,
					content: '是否提交!\r\n当前上架数量：'+this.inv[0].prtqty,
					success: function(res) {
						if(res.confirm){
							uni.showLoading({
							    title: '数据提交中',
								 mask:true
							});
							uni.request({
								url:this.serverURL_a + '/mo/AppApi/addOrderJl?__ajax=json',
								data:{
									dataMjl : inv,
									__sid:this.userInfo.sessionid
								},
								header: {
									'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
								},
								method: 'POST',
								success:function(data){
									uni.hideLoading();
									this.isReC = false;
									var request = data.data;
									if(data.statusCode!="200"){
										uni.showModal({
										    content: request.message,
											showCancel:false,
										});
										this.ishow = false;
										this.makeSound("sb");
										return;
									}
									if(request.result=="true"){
											this.inv = [{
												mocode:'',
												sortseq:'',
												cinvcode:'',
												cinvname:'',
												qty:'',
												prtqty:'',
											}];
											this.moData = [];
											this.$api.msg('提交成功!');
											this.makeSound("cg");
											return;
									}else{
										uni.showModal({
										    content: request.message,
											showCancel:false,
										});
										this.makeSound("sb");
										return;
									}
								}.bind(this),
								fail:function() {
									this.$api.msg('无法连接服务，请联系管理');
									this.isReC = false;
									return;
								}.bind(this)
							})
						}else if (res.cancel) {
							this.isReC = false;
							return
						}
					}.bind(this)
				});
			},
			//点击清空按钮
			clerInv(){
				this.hasLogins();
				uni.showModal({
					content: "是否清空所有存货信息!",
					success: function(res) {
						if (res.confirm) {
							this.inv = [{
								mocode:'',
								sortseq:'',
								cinvcode:'',
								cinvname:'',
								qty:'',
								prtqty:'',
							}];
							this.moData = [];
						}else if (res.cancel) {
						return
						}
					}.bind(this)
				});
			},
			//点击删除按钮
			toBox(index){
				this.hasLogins();
				this.inv.splice(index, 1);
				this.moData.splice(index, 1);
				if(this.inv.length < 1){
					this.inv = [{
						mocode:'',
						sortseq:'',
						cinvcode:'',
						cinvname:'',
						qty:'',
						prtqty:'',
					}];
				}
			},
			//调用手机扫码接口
			toScan(){
				uni.scanCode({
					onlyFromCamera: true,
					success: function(res) {
						var rdata = res.result;
						setTimeout(function(){
							this.moid = rdata;
							this.sub();
						}.bind(this),100);
						
					}.bind(this)
				});
			}
		}
	}
</script>

<style>
indexes {
		position: relative;
	}
/* 底部栏 */
.action-section{
	/* #ifdef H5 */
	/* #endif */
	position:fixed;
	left: 75%;
	bottom:30upx;
	z-index: 95;
	display: flex;
	align-items: center;
	background: rgba(255,255,255,.9);
	border-radius: 40upx;
	.checkbox{
		height:52upx;
		position:relative;
		image{
			width: 52upx;
			height: 100%;
			position:relative;
			z-index: 5;
		}
	}
	.clear-btn{
		position:absolute;
		left: 26upx;
		top: 0;
		z-index: 4;
		width: 0;
		height: 52upx;
		line-height: 52upx;
		padding-left: 38upx;
		font-size: $font-base;
		color: #fff;
		background: $font-color-disabled;
		border-radius:0 50px 50px 0;
		opacity: 0;
		transition: .2s;
		&.show{
			opacity: 1;
			width: 120upx;
		}
	}
	.total-box{
		flex: 1;
		display:flex;
		flex-direction: column;
		text-align:right;
		padding-right: 40upx;
		.price{
			font-size: $font-lg;
			color: $font-color-dark;
		}
		.coupon{
			font-size: $font-sm;
			color: $font-color-light;
			text{
				color: $font-color-dark;
			}
		}
	}
	.confirm-btn{
		padding: 0 38upx;
		margin: 0;
		border-radius: 100px;
		height: 76upx;
		line-height: 76upx;
		font-size: $font-base + 2upx;
		background: $uni-color-primary;
	}
}
</style>
